﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LookAHead
{
    class MemBasedH
    {
        public double[,] PdbCost;
        public Cell CenterCity;

        public MemBasedH(Cell _CenterCity)
        /* constructor of Mbh*/
        {
            CenterCity = new Cell(_CenterCity);
            PdbCost = new double[program.GridBoard.MaxX, program.GridBoard.MaxY];
            program.GridBoard.goal = CenterCity;
            program.GridBoard.goal.ChangeToOpen(); //goal will be the CenterCity
            for (int i = 0; i < program.GridBoard.MaxX; i++)
                for (int j = 0; j < program.GridBoard.MaxY; j++) //for each cell calculate h for the new goal
                    program.GridBoard.Get(i, j).CalcH(program.GridBoard.goal);
            for (int i = 0; i < program.GridBoard.MaxX; i++)
                for (int j = 0; j < program.GridBoard.MaxY; j++) //for each cell in grid run Astar
                    if (program.GridBoard.Get(i, j).state != Cell.states.obstacle)
                    {
                        program.GridBoard.start = program.GridBoard.Get(i, j);//i,j will be start
                        program.GridBoard.start.ChangeToStart();
                        program.RunAstar();
                        PdbCost[i, j] = program.TotalSolCost[program.AstarNum, 0];
                        program.TotalSolCost[program.AstarNum, 0] = 0;
                        program.GridBoard.ClearGrid();
                        program.GridBoard.goal.ChangeToEmpty();
                        program.GridBoard.start.ChangeToEmpty();
                    }

        }
        public MemBasedH(int x, int y)
        {
            this.CenterCity = program.GridBoard.Get(x, y);
            PdbCost = new double[program.GridBoard.MaxX, program.GridBoard.MaxY];
        }
        public void AddH(int i, int j, double h)
        {
            this.PdbCost[i, j] = h;
        }
        public bool IsEqual(MemBasedH other)
        {
            return this.CenterCity.IsEqual(other.CenterCity);
        }
    }
}
